package com.ryskoo.customer.vendorinfo.service;

import com.ryskoo.basicdata.area.city.domain.CityPO;
import com.ryskoo.basicdata.area.city.service.ICityService;
import com.ryskoo.basicdata.area.province.domain.ProvincePO;
import com.ryskoo.basicdata.area.province.service.IProvinceService;
import com.ryskoo.basicdata.area.region.domain.RegionPO;
import com.ryskoo.basicdata.area.region.service.IRegionService;
import com.ryskoo.customer.vendor.domain.VendorPO;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.customer.vendorinfo.domain.VendorInfoPO;
import com.ryskoo.customer.vendorinfo.repository.IVendorInfoRepository;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 会员业务基础处理
 * 
 * @author huanghe
 *
 */
@Service
@Transactional
public class VendorInfoServiceImpl extends BasicServiceImpl<VendorInfoPO> implements IVendorInfoService {

	@Autowired
	private IVendorInfoRepository VendorInfoRepository;

	@PostConstruct
	public void init() {
		this.setDao(VendorInfoRepository);
	}

	@Autowired
	private IProvinceService provinceService;
	@Autowired
	private ICityService cityService;
	@Autowired
	private IRegionService regionService;
	@Autowired
	private IVendorService vendorService;

	/**
	 * 根据VendorUuids 查询 VendorInfo
	 * 
	 * @param vendorUuids
	 * @return
	 */
	@Override
	public List<VendorInfoPO> getByVendorUuids(List<String> vendorUuids) {
		return VendorInfoRepository.getByVendorUuids(vendorUuids);
	}

	/**
	 * 根据 VendorUuid 查询 vendorInfo
	 * 
	 * @param vendorUuid
	 * @return
	 */
	@Override
	public VendorInfoPO getByVendorUuid(String vendorUuid) {
		VendorInfoPO info = VendorInfoRepository.getByVendorUuid(vendorUuid);
		if (null != info) {
			if(!StringUtils.isEmpty(info.getContactProvince())){
				ProvincePO provincePO = provinceService.getByUuid(info.getContactProvince());
				info.setContactProvinceName(provincePO == null ? "" : provincePO.getName());
			}
			if(!StringUtils.isEmpty(info.getContactCity())){
				CityPO cityPO = cityService.getByUuid(info.getContactCity());
				info.setContactCityName(cityPO == null ? "" : cityPO.getName());
			}
			if(!StringUtils.isEmpty(info.getContactRegion())){
				RegionPO regionPO = regionService.getByUuid(info.getContactRegion());
				info.setContactRegionName(regionPO == null ? "" : regionPO.getName());
			}
		}
		return info;
	}

	/**
	 * 根据 VendorUuids 删除 VendorInfo
	 * 
	 * @param needDeleteVendorUuids
	 * @return
	 */
	@Override
	public void deletesByVendorUuids(List<String> needDeleteVendorUuids) {
		VendorInfoRepository.deletesByVendorUuids(needDeleteVendorUuids);
	}

	/**
	 * 修改店铺Logo及店铺信息
	 */
	@Override
	public void updateVendorInfo(VendorPO vendor) {
		super.update(vendor.getVendorInfo());
		vendorService.update(vendor);
	}

}
